package de.arnoldmedia.mybookshelf;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.text.MessageFormat;

import javax.xml.bind.JAXBException;

import com.skype.SkypeException;

import de.arnoldmedia.mybookshelf.connector.SkypeConnector;
import de.arnoldmedia.mybookshelf.resources.Constants;
import de.arnoldmedia.mybookshelf.resources.ResourceHandler;
import de.arnoldmedia.mybookshelf.resources.model.Book;

/**
 * Main class of application
 * establish connection to Skype framework and contains the business logic 
 * @author aarnold
 * @date 21.02.2009
 * @version 1.0
 */
public class MyBookshelf implements IMyBookshelf {

	private static MyBookshelf singleton;
	
	private ResourceHandler resourceHandler = null;
	
	/**
	 * Connector Instance which holds connection to other clients
	 * @category class attribute
	 */
	private SkypeConnector connectionHandler = null;

	/**
	 * class constructor, called by main method
	 * @throws IOException 
	 * @throws FileNotFoundException 
	 * @category constructor
	 * @throws SkypeException
	 * @throws IOException 
	 * @throws FileNotFoundException 
	 * @throws SkypeException 
	 * @throws JAXBException 
	 */
	private MyBookshelf() throws FileNotFoundException, IOException, SkypeException {
		
		this.resourceHandler = new ResourceHandler( this );
		
		this.connectionHandler = new SkypeConnector( this , this.resourceHandler );

		// print out application name and version
		System.out.println( 
				MessageFormat.format(
						this.resourceHandler.getPropertyString( Constants.I18N_APPLICATION_STARTED ),
						this.getName()
					)
				);
	
	}//MyBookshelf

	/**
	 * main method of application
	 * @category main method
	 * @param args
	 * @throws SkypeException 
	 * @throws SkypeException
	 */
	public static void main(String[] args) throws SkypeException  {
		
		try {
			
			if ( singleton == null )
				singleton = new MyBookshelf();
			
//			try {
//				singleton.resourceHandler.loadBookshelf();
//			} catch (FileNotFoundException e1) {
//				// ignore
//			}
			
			singleton.resourceHandler.bookshelf.setBook(new Book("1","A","AA","4711"));
			singleton.resourceHandler.bookshelf.setBook(new Book("2","B","BB","911"));
			singleton.resourceHandler.saveBookshelf();
			
			singleton.find("911");
			
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (JAXBException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}	
	}//main
	
	
// ############## business methods ##################
	
	/* (non-Javadoc)
	 * @see de.arnoldmedia.mybookshelf.IBookshelf#getName()
	 */
	public String getName() {
		// must not return blanks to be used with Skype.addApplication() 
		return Constants.APPNAME +"_"+ Constants.VERSION;
	}//getName
	
	/* (non-Javadoc)
	 * @see de.arnoldmedia.mybookshelf.IBookshelf#find(java.lang.String)
	 */
	public void find(String isbn) throws SkypeException {
		
		// transmit given ISBN as search query to all connected clients
		// TODO this.connectionHandler.sendToAll("?isbn="+ isbn);
		assert this.connectionHandler != null;
		this.connectionHandler.sendToAll(isbn);
		
	}//find
	
	/* (non-Javadoc)
	 * @see de.arnoldmedia.mybookshelf.IBookshelf#lookUp(java.lang.String)
	 */
	public Integer lookUp(String isbn) {
		
		// look in local DB and return index if found
		assert this.resourceHandler != null;
		return this.resourceHandler.bookshelf.getBookIdx(isbn);
	
	}//lookUp
		
}//class
